package com.hcb.tonguetiphealth.widget.drinksbook;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hcb.tonguetiphealth.adapter.DrinksBookCartListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.model.Drinks;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class DrinksBookCartWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "DrinksBookCartWidget";
	
	private Activity ctx;
	private Handler mHandler;
	private Button bt_ok;
	private LinearLayout bt_back, bt_share;
	private TextView tv_privceTotal;
	private ListView drinksListView;
	private List<Drinks> drinksList;
	private DrinksBookCartListAdapter drinksAdapter;
	
	public DrinksBookCartWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.drinks_book_cart, this); 
		drinksList = Global.cartList;
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取酒水列表...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					drinksAdapter.notifyDataSetChanged();
					dismissMessageDialog();
					break;
				}
			}
		};
		tv_privceTotal = (TextView)findViewById(R.id.drinks_book_cart_price_total_textView);
		bt_back = (LinearLayout)findViewById(R.id.drinks_book_cart_back_button);
		bt_share = (LinearLayout)findViewById(R.id.drinks_book_cart_share_button);
		bt_ok = (Button)findViewById(R.id.drinks_book_cart_ok_button);
		bt_back.setOnClickListener(this);
		bt_share.setOnClickListener(this);
		bt_ok.setOnClickListener(this);
		
		drinksListView = (ListView)findViewById(R.id.drinks_book_cart_listview);
		initData();
		drinksAdapter = new DrinksBookCartListAdapter(this, ctx, drinksList);
		drinksListView.setAdapter(drinksAdapter);
	}
	
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.drinks_book_cart_back_button:
			ctx.setContentView(new DrinksBookWidget(ctx));
			break;
		case R.id.drinks_book_cart_share_button:
			//推荐享友
			
			break;
		case R.id.drinks_book_cart_ok_button:
			if(Global.cartList == null || Global.cartList.size() == 0) {
				showMessage(ctx, "购物车中暂无商品");
			} else {
				if(Global.user == null) {
					login();
				} else {
					ctx.setContentView(new DrinksBookAddressInfoWidget(ctx));
				}
			}
			break;
		default:
			break;
		}
	}
	
	private void initData() {
		changePriceTotal();
	}
	public void changePriceTotal() {
		float priceTotal = 0;
		if(drinksList != null) {
			for(Drinks drinks : drinksList) {
				priceTotal += drinks.getPriceTip() * drinks.getNum();
			}
		}
		tv_privceTotal.setText(priceTotal + "");
	}
	@Override
	public void notifyAdapter() {
		
	}
}
